package com.wbl.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nimbusds.jose.JOSEException;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.member.dto.MemberDto;
import com.wbl.train.common.member.entity.MemberEntity;
import com.wbl.train.common.member.vo.MemberVo;
import com.wbl.train.common.result.ResultCode;
import com.wbl.train.common.utils.JwtUtil;
import com.wbl.train.common.utils.SnowUtil;
import com.wbl.train.member.mapper.MemberMapper;
import com.wbl.train.member.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 会员 服务实现类
 * @author 王博纶
 * @since 2023-12-19
 */
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {

    @Override
    public boolean register(MemberEntity memberEntity) {
        // 使用BeanUtil进行类型转换
        MemberDto memberDto = BeanUtil.copyProperties(memberEntity, MemberDto.class);
        //构造查询条件
        MemberEntity member = getMemberByMobile(memberDto);
        //查询该手机号是否进行过注册
        if(member != null ){
            throw new ApplicationException(ResultCode.DATA_EXIST,"手机号已注册");
        }
        //进行数据库的添加，完成注册功能
        MemberEntity entity = new MemberEntity()
                .setId(SnowUtil.getSnowflakeNextId())
                .setMobile(memberEntity.getMobile());
        return this.save(entity);
    }

    @Override
    public MemberVo sendCode(MemberEntity memberEntity) {
        MemberDto memberDto = BeanUtil.copyProperties(memberEntity, MemberDto.class);
        MemberEntity member = getMemberByMobile(memberDto);
        // 手机号不存在先进行注册
        if(ObjectUtil.isNull(member)){
            log.info("手机号不存在,插入一条数据进行注册");
            //进行数据库的添加，完成注册功能
            MemberEntity entity = new MemberEntity()
                    .setId(SnowUtil.getSnowflakeNextId())
                    .setMobile(memberDto.getMobile());
            // 进行数据的插入
            this.save(entity);
        }
        //生成验证码
//        String yanKing = RandomUtil.randomString(6);
        String yanKing = "123456";
        log.info("生成短信验证码：{}",yanKing);
        return BeanUtil.copyProperties(memberEntity, MemberVo.class).setYanKing(yanKing);
    }

    @Override
    public MemberVo login(MemberDto memberDto) throws JOSEException {
        MemberEntity member = getMemberByMobile(memberDto);
        // 判断当前用户是否存在
        if(ObjectUtil.isNull(member)){
            // 对应的手机号不存在
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"请先获取短信验证码");
        }
        // 校验短信验证码
        if(!"123456".equalsIgnoreCase(memberDto.getYanKing())){
            //验证码不匹配
            throw new ApplicationException(ResultCode.DATA_ERROR,"验证码错误");
        }
        // 获取token字符串
//        String token = JwtTokenUtil.generateTokenByRSA(member.getMobile());
        String token = JwtUtil.createToken(member.getId(),member.getMobile());
        log.info("token: {}", token);
        MemberVo memberVo = BeanUtil.copyProperties(member, MemberVo.class);
        // 将token信息封装到memberVo中
        memberVo.setToken(token);
        return memberVo;
    }


    /**
     * 根据手机号查询用户
     * @param memberDto 会员的Vo类
     * @return 用户信息
     */
    private MemberEntity getMemberByMobile(MemberDto memberDto) {
        // 查询手机号是否存在
        LambdaQueryWrapper<MemberEntity> wrapper = new LambdaQueryWrapper<MemberEntity>()
                .eq(MemberEntity::getMobile, memberDto.getMobile());
        return this.getOne(wrapper);
    }
}
